package net.qhzw.vip.qiyu.controller;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import net.qhzw.vip.qiyu.entity.MaintRpt;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.MailService;
import net.qhzw.vip.qiyu.service.MaintRptService;
import net.qhzw.vip.qiyu.timer.RepairOrderTask;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.UUID;

/**
 * @title: TestController
 * @Author zhang
 * @Date: 2022/7/4 16:00
 * @Version 1.0
 */
@Controller
@RequestMapping("/test")
@Api(tags = "测试接口")
public class TestController {

    @Autowired
    private RepairOrderTask repairOrderTask;

    @Autowired
    private MaintRptService maintRptService;


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MailService mailService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Autowired
    private JavaMailSender javaMailSender;



    @Value("${spring.mail.username}")
    private String username;

    @ResponseBody
    @GetMapping("sendMail")
    public JsonResult sendMail(String address) throws UnsupportedEncodingException, MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        String nickName = MimeUtility.encodeText("青海骑遇网络科技有限公司");
        helper.setFrom(new InternetAddress(nickName + " <" + username + ">"));
        //收件人邮件地址
        helper.setTo(address);
        //邮件主题
        helper.setSubject(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + " 加急维修工单");
        //邮件正文
        String text = UUID.randomUUID().toString().replaceAll("-", "");
        helper.setText(text, true);
        javaMailSender.send(mimeMessage);
        return JsonResult.ok();
    }


    @GetMapping
    public JsonResult test(@RequestBody MaintRpt param) {
        Long userId = SecurityUtils.getUserId();
        System.out.println("存入维修队列");
        String jsonString = JSON.toJSONString(param);
        redisTemplate.boundHashOps("maintQueue").put(userId, jsonString);
        return JsonResult.ok();
    }


//    @RequestMapping("/dispatchNotice")
//    public String send(Model model, MaintRpt maintRpt, String admin) {
//        model.addAttribute("info", maintRpt);
//        model.addAttribute("admin", admin);
//        return "dispatchNotice";
//    }


//    @ResponseBody
//    @GetMapping("send")
//    public JsonResult send(String address) throws MessagingException {
//        String admin = "admin";
//        MaintRpt maintRpt = new MaintRpt();
//        maintRpt.setId(IdWorker.getId());
//        maintRpt.setRealName("张三");
//        maintRpt.setMobile("110");
//        maintRpt.setAddress("青海西宁");
//        maintRpt.setRemarks("我就是玩儿");
//        DispatchDto dispatchDto = new DispatchDto(maintRpt, admin);
//        mailService.sendHtmlMail(address, dispatchDto, "");
//        return JsonResult.ok();
//    }


    public static void main(String[] args) {
        System.out.println(new BCryptPasswordEncoder().encode("123456"));
    }
}
